WEBVTT

00:01.040 --> 00:05.310
In the last couple of videos we extensively worked with rolling statistics.

00:05.330 --> 00:11.660
For instance we calculated the simple moving average of stock prices and also the rolling mean return

00:11.990 --> 00:17.260
and the rolling standard deviation of returns and actually all examples.

00:17.260 --> 00:22.520
For one thing in common they're rolling statistics I calculated on one time series.

00:22.520 --> 00:28.850
So for example on the Apple stock price and in this video we will learn how to calculate rolling statistics

00:28.850 --> 00:34.490
like correlation or the covariance where we need to operate on two time series.

00:34.490 --> 00:41.960
So for example we could to calculate the rolling correlation of Apple monthly returns with the Boeing

00:41.960 --> 00:43.740
monthly returns.

00:43.760 --> 00:51.770
So now let's start here and as always import pandas and met plot lip and we use the Seabourn style and

00:51.770 --> 00:58.460
then we import our six stocks that we have in our stock so yes we fire and we create a daytime index

00:58.490 --> 01:01.850
and we only select here the close column.

01:02.150 --> 01:04.380
So let's do this and let's have a look here.

01:04.400 --> 01:05.950
So these are our stocks.

01:06.080 --> 01:10.600
Apple Boeing Disney IBM Coca-Cola and Microsoft.

01:10.700 --> 01:15.680
And now in the following minutes we want to calculate the rolling correlation between the Apple stock

01:16.130 --> 01:17.270
and the Boeing stock.

01:17.270 --> 01:26.010
And therefore we only select the apple and Boeing and from our time period from end of 2009 and the

01:26.090 --> 01:32.760
end of 2000 and 18 and we actually created a new data frame and called the data frame.

01:32.810 --> 01:34.350
Apple Boeing.

01:34.550 --> 01:37.120
So let's do this and let's have a look.

01:37.130 --> 01:41.630
So here we have the two columns Apple and Boeing with the stock prices.

01:41.630 --> 01:48.020
And I want a first step we calculate the monthly returns and therefore first of all we have to re sample

01:48.020 --> 01:54.950
the data frame to a monthly and we pass a period to the kind parameter and we take actually the very

01:54.950 --> 02:01.570
last the trading price of each month and then with the percentage change method we actually calculate

02:01.570 --> 02:05.600
the simple returns and we are actually saving.

02:05.630 --> 02:10.080
The new returns data frame and the variable monthly returns.

02:10.210 --> 02:11.010
So let's have a look.

02:13.060 --> 02:21.730
So here we have the monthly returns for Apple and Boeing starting in January 2010 and December 2000

02:21.790 --> 02:23.350
and 18.

02:23.650 --> 02:26.480
And that's also the a look here with the info method.

02:26.530 --> 02:33.830
So we have a total one hundred eight monthly returns for nine years times twelve months gives us one

02:33.830 --> 02:40.540
hundred eight monthly returns and then we could simply calculate the correlation between the apple return

02:40.900 --> 02:44.860
and the Boeing return for the whole nine year period.

02:44.860 --> 02:53.340
So by using here the com method let's do this year and no surprise there that we have a perfect positive

02:53.340 --> 02:58.020
correlation between the Apple stock and the Apple stock and the Boeing stock and the Boeing stock.

02:58.470 --> 03:03.730
But here we can see uh the correlation of the last nine years.

03:03.750 --> 03:07.440
The monthly returns between Apple and Boeing.

03:07.500 --> 03:10.870
And this is the kind of positive so point to two.

03:11.190 --> 03:18.240
And I wouldn't say that this is a strong positive correlation but still it's a significant uh positive

03:18.240 --> 03:19.620
correlation.

03:19.620 --> 03:24.410
So that's the significant trend that whenever the apple return is positive.

03:24.420 --> 03:29.390
Then also the Boeing return is positive and vice versa.

03:29.430 --> 03:36.990
So this is the correlation for the complete one hundred eight month period and the same with the returns

03:37.020 --> 03:38.730
and standard deviations.

03:38.730 --> 03:43.040
Let's assume that you want to calculate the rolling correlation.

03:43.290 --> 03:45.250
And also here we can define that.

03:45.300 --> 03:49.050
We want to have a window of 36 months.

03:49.170 --> 03:56.670
So it's quite common in the finance industry to calculate return and standard deviation outlaw correlation

03:56.820 --> 04:04.200
based on monthly returns of uh the last uh 36 six months and coding wise we can actually start to do

04:04.200 --> 04:07.200
so by selecting here the apple return.

04:07.620 --> 04:15.750
Then we apply the rolling method and we pass the thirty six uh to the windows parameter and we already

04:15.750 --> 04:16.390
know that.

04:16.440 --> 04:21.260
Now we can for example calculate the rolling mean return by chaining here.

04:21.270 --> 04:22.230
The mean method.

04:24.930 --> 04:26.580
So this is nothing new.

04:26.610 --> 04:32.580
So this is the three years uh rolling mean return for the Apple stock.

04:32.580 --> 04:39.990
And we also have calculated the rolling standard deviation of returns by chaining here the standard

04:39.990 --> 04:41.220
deviation method.

04:41.220 --> 04:44.370
So this is actually nothing new.

04:44.520 --> 04:50.190
And now we want to calculate that the rolling correlation and it's actually no surprise that we have

04:50.190 --> 04:56.680
to change the car method and inside the parenting is this.

04:56.730 --> 05:03.390
Now we have to decide for which time seriously want to have the role in correlation to the Apple return.

05:03.390 --> 05:08.490
And if we can simply pass the panda series with the Boeing returns.

05:08.490 --> 05:17.530
So it's here monthly return the data frame and then we select uh the Boeing returns.

05:17.790 --> 05:25.010
And now let's run here the cell and it's actually no surprise that here for the first the thirty five

05:25.050 --> 05:27.060
data points we have any end values.

05:27.480 --> 05:34.320
But then here in the very end we have actually the thirty six month rolling correlation between the

05:34.410 --> 05:37.860
Apple stock return and the Boeing stock return.

05:38.250 --> 05:42.830
And as in most cases the graphs are more intuitive than a lot of numbers.

05:42.870 --> 05:48.580
So therefore we can also plot here and visualize a reading role rolling correlation.

05:48.630 --> 05:52.410
So we use here the plot method and let's have a look.

05:56.310 --> 06:00.940
So this is of the year all in correlation between APA returns and Boeing returns.

06:01.230 --> 06:10.240
And we have seen before that the average over the nine year period is 0 point 2 to so the average C

06:10.240 --> 06:11.940
is somewhere here.

06:11.940 --> 06:15.780
But we can also see here that the correlation is quite volatile.

06:16.290 --> 06:21.300
So we have fear at the end of the year 2000 and 13.

06:21.600 --> 06:24.040
Even a negative correlation.

06:24.220 --> 06:31.680
And here into a 16 and 17 we have a correlation of 0 point 5 and actually currently we are quite close

06:31.890 --> 06:34.250
to the mean of 0 point to 2.

06:34.250 --> 06:40.380
So here we are currently somewhere at 0 point 1 8 0.01 7.

06:41.070 --> 06:42.960
And actually the key message is here.

06:42.960 --> 06:49.080
Same with return and risk that also the correlation heavily depends on the market environment and on

06:49.080 --> 06:50.410
the timing.

06:50.430 --> 06:53.690
So these metrics are actually quite volatile over time.

06:54.090 --> 07:00.320
And having only a look at one single timestamp might not give us the complete information.

07:00.570 --> 07:08.340
So it's also quite it to have a look at the rolling statistics and with this we are finished here and

07:08.340 --> 07:10.890
I hope to see you also in the next video by.
